# coding=utf8
from app.extensions import db
from app.modules.common.utils import ComplexEncoder
from sqlalchemy import func
from app.modules.common.dbUtil import createId

"""
延期记录
"""


class PostponeRecord(db.Model, ComplexEncoder):
    __tablename__ = "postpone_record"
    id = db.Column(db.String, primary_key = True)
    create_date = db.Column(db.DateTime, default = func.now())
    postpone_user_id = db.Column(db.String, nullable = False)
    postpone_user_name = db.Column(db.String, nullable = False)
    apply_id = db.Column(db.String, nullable = False)
    agent_ip = db.Column(db.String, nullable = False)
    from_date = db.Column(db.Date, nullable = False)
    to_date = db.Column(db.Date, nullable = False)
    plan_name = db.Column(db.String)
    desc = db.Column(db.String)

    def __init__(self, postpone_user_id, postpone_user_name, apply_id, agent_ip, from_date, to_date, plan_name, desc):
        self.id = createId('p')
        self.postpone_user_id = postpone_user_id
        self.postpone_user_name = postpone_user_name
        self.apply_id = apply_id
        self.agent_ip = agent_ip
        self.from_date = from_date
        self.to_date = to_date
        self.plan_name = plan_name
        self.desc = desc

    def to_dict(self):
        return {
            'id': self.id,
            'create_date': self.create_date.strftime('%Y-%m-%d %H:%M:%S') if self.create_date else None,
            'postpone_user_id': self.postpone_user_id,
            'postpone_user_name': self.postpone_user_name,
            'apply_id': self.apply_id,
            'agent_ip': self.agent_ip,
            'from_date': self.from_date.strftime('%Y-%m-%d') if self.from_date else None,
            'to_date': self.to_date.strftime('%Y-%m-%d') if self.to_date else None,
            'plan_name': self.plan_name,
            'desc': self.desc,
        }
